package com.haikesi.api.device.controller;

import com.github.pagehelper.PageInfo;
import com.haikesi.api.base.ApiIndex;
import com.haikesi.api.base.Error;
import com.haikesi.api.base.R;
import com.haikesi.api.base.Type;
import com.haikesi.api.base.annotation.Token;
import com.haikesi.api.device.domain.vo.*;
import com.haikesi.api.device.service.IDeviceService;
import com.haikesi.util.ApcosApiUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


@Api(description = "设备管理相关接口", tags = ApiIndex.DeviceController)
@RequestMapping(value = "/api/device")
@RestController
public class DeviceController {

    @Autowired
    private IDeviceService service;

    @ApiOperation(value = "查询列表")
    @GetMapping(value = "/list")
//    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
//    @Token
    public R<PageInfo<List<DeviceVO>>> list(
            @ApiParam(value = "查询参数") @ModelAttribute DeviceSearchVO searchVO) {
        List<DeviceVO> list = service.getList(searchVO);
        PageInfo pageInfo = new PageInfo(list);
        return new R(pageInfo);
    }


    @ApiOperation(value = "修改")
    @PutMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R update(
            @ApiParam(value = "修改参数") @Valid @RequestBody DeviceUpdateVO updateVO, BindingResult bindingResult) {
        if(bindingResult.hasErrors()) {
            List<Error> messages=new ArrayList<>();
            for(int i=0;i<bindingResult.getFieldErrors().size();i++){
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(),bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        service.update(updateVO);
        return new R();
    }

    @ApiOperation(value = "导入设备")
    @GetMapping(value = "importApcosDervice")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R importApcosDervice(
            @ApiParam(value = "被订阅的账号 ") @RequestParam String account
    ) {
        return  new R(service.importApcosDervice(account));
    }



    @ApiOperation(value = "控制设备")
    @PostMapping(value = "controlDervice")
//    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
//    @Token
    public R controlDervice(
            @ApiParam(value = "修改参数") @Valid @RequestBody DeviceControlVO deviceControlVO, BindingResult bindingResult
    ) {
        if(bindingResult.hasErrors()) {
            List<Error> messages=new ArrayList<>();
            for(int i=0;i<bindingResult.getFieldErrors().size();i++){
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(),bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        service.controlDervice(deviceControlVO);
        return  new R();
    }


    @ApiOperation(value = "控制设备（查询类设备，返回具体参数）")
    @PostMapping(value = "controlDervice_s")
//    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
//    @Token
    public R controlDervice_s(
            @ApiParam(value = "修改参数") @Valid @RequestBody DeviceControlVO deviceControlVO, BindingResult bindingResult
    ) {
        if(bindingResult.hasErrors()) {
            List<Error> messages=new ArrayList<>();
            for(int i=0;i<bindingResult.getFieldErrors().size();i++){
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(),bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        return service.controlDervice_s(deviceControlVO);
    }




    @ApiOperation(value = "批量修改设备机构")
    @PutMapping(value = "/batch_up_organization")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R batch_up_organization(
            @ApiParam(value = "修改参数") @Valid @RequestBody DeviceBatchUpdateVO updateVO, BindingResult bindingResult) {
        if(bindingResult.hasErrors()) {
            List<Error> messages=new ArrayList<>();
            for(int i=0;i<bindingResult.getFieldErrors().size();i++){
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(),bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        service.batch_up_organization(updateVO);
        return new R();
    }


    @ApiOperation(value = "批量修改设备位置")
    @PutMapping(value = "/batch_up_position")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R batch_up_position(
            @ApiParam(value = "修改参数") @Valid @RequestBody DeviceBatchUpdateVO updateVO, BindingResult bindingResult) {
        if(bindingResult.hasErrors()) {
            List<Error> messages=new ArrayList<>();
            for(int i=0;i<bindingResult.getFieldErrors().size();i++){
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(),bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        service.batch_up_position(updateVO);
        return new R();
    }


}
